package top.pmwly.oauth2_jwt.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenEnhancerChain;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import top.pmwly.oauth2_jwt.service.UserService;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * @author Herther
 * @version 1.0.0
 * @ClassName AuthenticationServerConfig.java
 * @Description 授权服务器配置类
 * @createTime 2021年08月23日 14:42:00
 */
@Configuration
@EnableAuthorizationServer
public class AuthenticationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private UserService userService;

    @Autowired
    @Qualifier("jwtTokenStore")
    private TokenStore tokenStore;

    @Autowired
    @Qualifier("jwtAccessTokenConverter1")
    private JwtAccessTokenConverter jwtAccessTokenConverter1;

    @Autowired
    private JwtEnhancer jwtEnhancer;


    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                //客户端id，方便演示放入内存
                .withClient("admin")
                //客户端秘密吗，方便演示放入内存
                .secret(passwordEncoder.encode("666"))
                //token 失效时间
                .accessTokenValiditySeconds(3600)
                //授权范围
                .scopes("all")
                //授权类型 为密码授权
                .authorizedGrantTypes("password","refresh_token","authorization_code");
    }

    /**
     * 密码授权的 配置端点
     * @param endpoints
     * @throws Exception
     */
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {

        //配置内容增强器
        TokenEnhancerChain tokenEnhancerChain =new TokenEnhancerChain();
        ArrayList<TokenEnhancer> delegates = new ArrayList<TokenEnhancer>();
        //强增加器放入到 集合中
        delegates.add(jwtEnhancer);
        //将转换器放入集合中
        delegates.add(jwtAccessTokenConverter1);
        //将集合 设置到 TokenEnhancerChain ，实现添加内容
        tokenEnhancerChain.setTokenEnhancers(delegates);
        //授权管理器
        endpoints.authenticationManager(authenticationManager)
                .userDetailsService(userService)
                //存储token 策略
                .tokenStore(tokenStore)
                //token转换
                .accessTokenConverter(jwtAccessTokenConverter1)
                .tokenEnhancer(tokenEnhancerChain);
    }
}
